import { Module } from 'vuex'
import { nextTick } from 'vue'

const cacheView: Module<Store.CacheViewState, Store.RootState> = {
  namespaced: true,
  state: {
    cacheViewList: [] // keep-alive 缓存页面
  },
  mutations: {
    SET_CACHE_VIEWS(state, names: string[]) {
      names.forEach(name => {
        if (!state.cacheViewList.includes(name)) state.cacheViewList.push(name)
      })
    },
    RESET_CACHE_VIEW(state, name: string) {
      const fintItem = state.cacheViewList.findIndex(item => item === name)
      if (fintItem !== -1) {
        state.cacheViewList.splice(fintItem, 1)
        nextTick(() => {
          if (!state.cacheViewList.includes(name)) state.cacheViewList.push(name)
        })
      }
    }
  }
}
export default cacheView
